﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AMS.DataAccess.Interfaces;
using AMS.DataAccess.Common;
using AMS.DataAccess.BO;
using AMS.DataAccess.Trademark.BO;
using AMS.DataAccess.BO.Enums;
using AMS.DataAccess.Trademark.Interfaces;

public partial class Cases_ucEditApplicantForCase : UCBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Utils.ParseIntQueryString(Request, "caseid", ref caseId);
        if (caseId != 0)
        {
            CaseID = caseId;
        }

        if (!IsPostBack)
        {
            BindDropdownLists();
        }
    }

    #region Properties
    int caseId;
    private int ApplicantID
    {
        get { return ViewState["ApplicantID"] == null ? 0 : (int)ViewState["ApplicantID"]; }
        set { ViewState["ApplicantID"] = value; }
    }

    private int CaseID
    {
        get { return ViewState["CaseID"] == null ? 0 : (int)ViewState["CaseID"]; }
        set { ViewState["CaseID"] = value; }
    }

    /// <summary>
    /// default value is add
    /// </summary>
    private string CurrentAction
    {
        get { return ViewState["CurrentAction"] == null ? "add" : (string)ViewState["CurrentAction"]; }
        set { ViewState["CurrentAction"] = value; }
    }
    #endregion

    #region Events
    public event EventHandler ApplicantAdded;
    protected void OnApplicantAdded()
    {
        if (ApplicantAdded != null)
        {
            ApplicantAdded(this, EventArgs.Empty);
        }
    }
    #endregion

    /// <summary>
    /// Show case Info
    /// </summary>
    /// <param name="applicantId"></param>
    /// <returns></returns>
    public void EditCaseApplicant(CaseApplicantInfo app)
    {
        txtAddress.Text = app.Address;
        txtCustomerCode.Text = app.IDCode;
        txtName.Text = app.Name;
        txtName_En.Text = app.Name_En;
        txtTel.Text = app.Tel;
        txtZipCode.Text = app.ZipCode;
        Utils.SelectDropDownList(drpCountry, app.CountryID);
        if (Utils.SelectDropDownList(drpProvince, app.ProvinceId))
        {
            BindCities(app.ProvinceId);
            Utils.SelectDropDownList(drpCity, app.CityId);
        }

        CurrentAction = "edit";
        ApplicantID = app.AID;
    }

    #region Bind dropdown lists
    void BindDropdownLists()
    {
        IProvinceCityManager pc = ManagerFactory.CreateProvinceManager();
        List<ProvinceCityInfo> provinces = pc.GetProvinces(CallContext);
        drpProvince.DataSource = provinces;
        drpProvince.DataTextField = "ProvinceName";
        drpProvince.DataValueField = "ProvinceID";
        drpProvince.DataBind();

        if (drpProvince.Items.Count > 0)
        {
            BindCities(drpProvince.Items[0].Value);
        }
    }

    private void BindCities(string provinceId)
    {
        IProvinceCityManager mgr = ManagerFactory.CreateProvinceManager();
        List<ProvinceCityInfo> cities = mgr.GetCitiesByProvince(CallContext, provinceId);
        if (cities.Count == 0)
        {
            cities.Add(new ProvinceCityInfo(drpProvince.SelectedValue, drpProvince.Text));
        }

        drpCity.DataSource = cities;
        drpCity.DataTextField = ProvinceCityInfo.NameFieldName;
        drpCity.DataValueField = ProvinceCityInfo.IDFieldName;
        drpCity.DataBind();
    }

    protected void drpProvince_DataBound(object sender, EventArgs e)
    {
        if (drpProvince.Items.Count > 0)
        {
            BindCities(drpProvince.SelectedValue);
        }
    }

    protected void drpProvince_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindCities(drpProvince.SelectedValue);
    }

    protected void drpCountry_DataBound(object sender, EventArgs e)
    {
        if (drpCountry.Items.Count > 0)
        {
            try
            {
                drpCountry.SelectedValue = Constants.ChinaCountryID;
            }
            catch (Exception)
            {
                drpCountry.SelectedIndex = 0;
            }
        }
    }
    #endregion

    bool RetriveCaseApplicantInfo(CaseApplicantInfo applicant)
    {
        applicant.BusinessType = CustomerBusinessType.Applicant;
        applicant.Name = txtName.Text;
        applicant.Name_En = txtName_En.Text;
        applicant.IDCode = txtCustomerCode.Text;
        applicant.CountryID = drpCountry.SelectedValue;
        applicant.CreateDate = DateTime.Now;

        if (drpProvince.Items.Count > 0)
        {
            applicant.ProvinceId = drpProvince.SelectedValue;
        }

        if (drpCity.Items.Count > 0)
        {
            applicant.CityId = drpCity.SelectedValue;
        }

        applicant.ZipCode = txtZipCode.Text;
        applicant.Address = txtAddress.Text;
        applicant.Tel = txtTel.Text;
        return true;
    }

    protected void btnOK_Click(object sender, EventArgs e)
    {
        CaseApplicantInfo applicant = null;

        // TODO: validate the applicant
        ICustomerManager cusMgr = ManagerFactory.CreateCustomerManager();
        ICaseApplicantManager caseAppMgr = ManagerFactory.CreateCaseApplicantManager();
        if (CurrentAction == "add")
        {
            applicant = new CaseApplicantInfo();
            RetriveCaseApplicantInfo(applicant);
            applicant.CaseID = CaseID;

            cusMgr.AddCustomer(CallContext, applicant);
            caseAppMgr.AddCaseApplicant(CallContext, applicant);
            OnApplicantAdded();
        }
        else
        {
            applicant = caseAppMgr.GetCaseApplicant(CallContext, CaseID, ApplicantID);
            if (applicant != null)
            {
                RetriveCaseApplicantInfo(applicant);
                cusMgr.UpdateCustomer(CallContext, applicant);
                MsgInfo("已更新申请人信息：" + applicant.Name);
                OnApplicantAdded();
            }
            else
            {
                MsgInfo("信息不存在，可能已经被删除。");
            }
        }
    }

    public override void Reset()
    {
        base.Reset();
        drpCountry.SelectedValue = Constants.ChinaCountryID;
        BindCities(drpProvince.SelectedValue);
        ApplicantID = 0;
        CurrentAction = null;
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        Reset();
    }
}
